//
//  AFNetWorkingW.m
//  gomente
//
//  Created by gomente on 15/1/19.
//  Copyright (c) 2015年 gomente. All rights reserved.
//

#import "AFNetWorkingW.h"
#import "AFHTTPSessionManager.h"
#import "AFHTTPRequestOperation.h"
#import "AFHTTPRequestOperationManager.h"
@implementation AFNetWorkingW
- (void)netWorkWithURL:(NSString *)urlStr body:(NSString *)body resultBlock:(void (^)(id result))block
{
    NSURL *url = [NSURL URLWithString:[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60];
    request.HTTPMethod = @"POST";
    // 需要提交的表单
    NSString *bodyStr = body;
    // 将bodyStr转为NSData类型
    NSData *bodyData = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
    // 设置请求body(NSData类型)
    request.HTTPBody = bodyData;
    //    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    op.securityPolicy.allowInvalidCertificates = YES;
    [op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *html = operation.responseString;
        NSData *data = [html dataUsingEncoding:NSUTF8StringEncoding]; //请求的数据
        id reuselt = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        block(reuselt);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        block(error);
        NSLog(@"%@",error);
    }];
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    [queue addOperation:op];
}

- (void)getDataWithUrl:(NSString *)urlStr resultBlock:(void(^)(id result))block
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.securityPolicy.allowInvalidCertificates = YES;
//    [manager.responseSerializer setAcceptableContentTypes: [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css", nil]];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager GET:urlStr parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *html = operation.responseString;
        NSData *data = [html dataUsingEncoding:NSUTF8StringEncoding]; //请求的数据
        id reuselt = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        block(reuselt);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];
}

+ (void)netWorkingHanderGetGataWithUrl:(NSString *)urlStr resultBlock:(void(^)(id result))block
{
    AFNetWorkingW *leaonWork = [[AFNetWorkingW alloc] init];
    [leaonWork getDataWithUrl:urlStr resultBlock:block];
}

+ (void)netWorkWithURL:(NSString *)urlStr body:(NSString*)body resultBlock:(void(^)(id))block
{
    AFNetWorkingW *yu = [[AFNetWorkingW alloc] init];
    [yu netWorkWithURL:urlStr body:body resultBlock:block];
}

+ (void)getNetState
{
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    // 检测网络连接的单例,网络变化时的回调方法
    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        NSLog(@"%ld", (long)status);
        if (status == AFNetworkReachabilityStatusUnknown && status == AFNetworkReachabilityStatusNotReachable) {
            NSLog(@"=======");
        }
    }];
}


@end
